// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'
import App from './App'
import router from './router/routers'
import iView from 'iview'
import './../static/css/iview.css'
import './../static/swiper/css/swiper.css'
import iviewArea from 'iview-area'
import axios from 'axios'
import Qs from 'qs'
import {post, put, get, deletes} from '_com/http'
import VueParticles from './components/vue-particles'
import VueClipboards from 'vue-clipboard2'
import VideoPlayer from 'vue-video-player'

Vue.prototype.$qs = Qs
Vue.prototype.$post = post
Vue.prototype.$put = put
Vue.prototype.$get = get
Vue.prototype.$deletes = deletes
Vue.config.productionTip = false

Vue.use(iView)
Vue.use(iviewArea)
Vue.use(VueParticles)
Vue.use(VueClipboards)
Vue.use(VideoPlayer)

axios.interceptors.request.use(
  config => {
    let token = localStorage.getItem('x-token')
    if (token) {
      config.headers['x-token'] = token
    }
    return config
  },
  err => {
    return Promise.reject(err)
  })
axios.interceptors.response.use(
  response => {
    return response
  },
  error => {
    if (error.response) {
      switch (error.response.status) {
        case 401:
          router.replace({
            path: '/index/login',
            query: {redirect: router.currentRoute.fullPath}
          })
          break
        case 500:
          if (error.response.data.message === '该请求需要先登录才可以访问,用户未登录或token不存在！') {
            router.replace({
              path: '/index/login'
            })
          }
          break
      }
    }
    return Promise.reject(error.response.data)
  })

/* eslint-disable no-new */
new Vue({
  el: '#app',
  router,
  render: h => h(App)
})
